from fastapi import FastAPI

from dc_shop.config import Settings
from dc_shop.core.constants import LOCALES_FOLDER
from dc_shop.database.main import build_sa_engine, build_sa_session_factory
from dc_shop.database.services import BotSettingsService
from dc_shop.database.services.discount import DiscountService
from dc_shop.database.services.order import OrderService
from dc_shop.database.services.product import ProductService
from dc_shop.database.services.promocode import PromocodeService
from dc_shop.database.services.shop import ShopService
from dc_shop.database.services.user import UserService
from dc_shop.database.uow import SQLAlchemyUoW
from dc_shop.database.uow.infra import (
    InfraProvider,
    get_bot_settings_service,
    get_discount_service,
    get_order_service,
    get_product_service,
    get_promocode_service,
    get_shop_service,
    get_user_service,
)
from dc_shop.tg_bot.services.locale import Localizator, configure_localizator
from dc_shop.webapp.app.providers.stub import Stub


def setup_providers(
    app: FastAPI,
    config: Settings,
) -> None:
    engine = build_sa_engine(config.db)

    session_factory = build_sa_session_factory(engine)

    provider = InfraProvider(session_factory=session_factory)
    localizator = configure_localizator(LOCALES_FOLDER)

    app.dependency_overrides[Stub(SQLAlchemyUoW)] = provider.get_uow
    app.dependency_overrides[Stub(ProductService)] = get_product_service
    app.dependency_overrides[Stub(UserService)] = get_user_service
    app.dependency_overrides[Stub(ShopService)] = get_shop_service
    app.dependency_overrides[Stub(DiscountService)] = get_discount_service
    app.dependency_overrides[Stub(OrderService)] = get_order_service
    app.dependency_overrides[Stub(PromocodeService)] = get_promocode_service
    app.dependency_overrides[Stub(BotSettingsService)] = get_bot_settings_service
    app.dependency_overrides[Stub(Settings)] = lambda: config
    app.dependency_overrides[Stub(Localizator)] = lambda: localizator
